package cn.com.thingo.hyintelligent.presenter;

import com.videogo.exception.BaseException;
import com.videogo.openapi.EZOpenSDK;
import com.videogo.openapi.bean.EZDeviceInfo;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import cn.com.thingo.hyintelligent.app.App;
import cn.com.thingo.hyintelligent.base.RxPresenter;
import cn.com.thingo.hyintelligent.base.contract.MonitorContract;
import cn.com.thingo.hyintelligent.model.DataManager;
import cn.com.thingo.hyintelligent.model.bean.EzAccountBean;
import cn.com.thingo.hyintelligent.model.bean.GetEzTokenBean;
import cn.com.thingo.hyintelligent.model.bean.UserBean;

import cn.com.thingo.hyintelligent.util.LogUtil;
import cn.com.thingo.hyintelligent.util.RxUtil;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class MonitorPresenter extends RxPresenter<MonitorContract.View> implements MonitorContract.Presenter {

    private DataManager dataManager;

    @Inject
    public MonitorPresenter(DataManager dataManager) {
        this.dataManager = dataManager;
    }

    @Override
    public void getDeviceInfoList(final String ezAccessToken) {

        Observable.create(new ObservableOnSubscribe<List<EZDeviceInfo>>() {
            @Override
            public void subscribe(ObservableEmitter<List<EZDeviceInfo>> e) throws Exception {
                List<EZDeviceInfo> infos = new ArrayList<>();
                if (ezAccessToken != null) {
                    try {
                        infos = EZOpenSDK.getInstance().getDeviceList(0, 20);
                    } catch (BaseException e1) {
                        e1.printStackTrace();
                    }
                    e.onNext(infos);
                } else {
                    e.onNext(infos);
                }
            }
        }).subscribeOn(Schedulers.newThread()).observeOn(
                AndroidSchedulers.mainThread()
        ).subscribe(new Consumer<List<EZDeviceInfo>>() {
            @Override
            public void accept(List<EZDeviceInfo> deviceInfos) throws Exception {
                mView.showDeviceInfoList(deviceInfos);
            }

        });

    }










}
